﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ASPNET.StarterKit.BusinessLogicLayer;
using ASPNET.StarterKit.DataAccessLayer;
using System.Data.SqlClient;



public partial class InventoryTracker_Location : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSupNew.Enabled = true;
        btnSupDelete.Enabled = false;
        btnSupEdit.Enabled = false;
        btnSubmit.Enabled = true;
        btnSupUpdate.Enabled = false;

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtSupFax.Text == null || txtSupFax.Text == "")
        {
            txtSupFax.Text = "0";
        }
        if (txtSupEMail.Text == null || txtSupEMail.Text == "")
        {
            txtSupEMail.Text = "-";
        }
        if (txtSupLeadTime.Text == null || txtSupLeadTime.Text == "")
        {
            txtSupLeadTime.Text = "0";
        }
        

        DataAccess DA = DataAccessHelper.GetDataAccess();
        Supplier aSupplier = new Supplier(txtSupName.Text, txtSupStreet.Text, txtSupCity.Text, txtSupState.Text, txtSupCountry.Text, Int64.Parse(txtSupZipCode.Text), Int64.Parse(txtSupPhone.Text), Int64.Parse(txtSupFax.Text), txtSupEMail.Text, int.Parse(txtSupLeadTime.Text), RDbtnListSupp.SelectedValue);
            //int.Parse( txtSupStatus.Text));
        try
        {
            if (DA.CreateSupplier (aSupplier))
            {
                lblResult.Text = "Success";
            }
            else
            {
                lblResult.Text = "Failure";
            }
        }
        catch (Exception ex)
        {
            lblResult.Text = ex.Message.ToString();
        }



    }
    protected void DdlSupplierName_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnSupNew.Enabled = true;
        btnSupDelete.Enabled = true;
        btnSupEdit.Enabled = true;
        btnSubmit.Enabled = false;
        btnSupUpdate.Enabled = false;


        String SelectValue = DdlSupplierName.SelectedValue;

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "SELECT * FROM SupplierMaster WHERE  [SupID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        DataTable dataTable = GetDataTable(cmd);
        txtSupName.Text = dataTable.Rows[0]["SupName"].ToString(); 
        txtSupStreet.Text = dataTable.Rows[0]["SupAddrStreet"].ToString();
        txtSupCity.Text = dataTable.Rows[0]["SupAddrCity"].ToString();
        txtSupState.Text = dataTable.Rows[0]["SupState"].ToString();
        txtSupCountry.Text = dataTable.Rows[0]["SupCountry"].ToString();
        txtSupZipCode.Text  = dataTable.Rows[0]["SupZipCode"].ToString();
        txtSupPhone.Text = dataTable.Rows[0]["SupPhone"].ToString();
        txtSupFax.Text = dataTable.Rows[0]["SupFax"].ToString();
        txtSupEMail.Text = dataTable.Rows[0]["SupEmailId"].ToString();
        txtSupLeadTime.Text = dataTable.Rows[0]["SupLeadTime"].ToString();
        //txtSupStatus.Text = dataTable.Rows[0]["SupStatus"].ToString();
        RDbtnListSupp.SelectedValue = dataTable.Rows[0]["SupStatus"].ToString();
        con.Close();
    }

    public static DataTable GetDataTable(SqlCommand cmd)
    {

        SqlDataAdapter da = new SqlDataAdapter();
        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;
        // create a new DataTable
        DataTable dtGet = new DataTable();
        //fill the DataTable
        da.Fill(dtGet);
        //return the DataTable
        return dtGet;

    }

    protected void btnSupNew_Click1(object sender, EventArgs e)
    {
        btnSupNew.Enabled = false;
        btnSupDelete.Enabled = false;
        btnSupEdit.Enabled = false;
        btnSubmit.Enabled = true;
        btnSupUpdate.Enabled = false;

        txtSupName.Text = "";
        txtSupStreet.Text = "";
        txtSupCity.Text = "";
        txtSupState.Text = "";
        txtSupCountry.Text = "";
        txtSupZipCode.Text = "";
        txtSupPhone.Text = "";
        txtSupFax.Text = "";
        txtSupEMail.Text = "";
        txtSupLeadTime.Text = "";
        //txtSupStatus.Text = "";
        RDbtnListSupp.SelectedIndex = 0;


    }
    protected void btnSupDelete_Click1(object sender, EventArgs e)
    {
        String SelectValue = DdlSupplierName.SelectedValue;

        btnSupNew.Enabled = false;
        btnSupDelete.Enabled = true;
        btnSupEdit.Enabled = false;
        btnSubmit.Enabled = false;
        btnSupUpdate.Enabled = false;
        
        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "DELETE FROM SupplierMaster WHERE  [SupID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtSupName.Text = "";
        txtSupStreet.Text = "";
        txtSupCity.Text = "";
        txtSupState.Text = "";
        txtSupCountry.Text = "";
        txtSupZipCode.Text = "";
        txtSupPhone.Text = "";
        txtSupFax.Text = "";
        txtSupEMail.Text = "";
        txtSupLeadTime.Text = "";
        //txtSupStatus.Text = "";
        RDbtnListSupp.SelectedIndex = 0;
    }
    protected void btnSupEdit_Click(object sender, EventArgs e)
    {
        btnSupNew.Enabled = false;
        btnSupDelete.Enabled = false;
        btnSupEdit.Enabled = false;
        btnSubmit.Enabled = false;
        btnSupUpdate.Enabled = true;


        

    }
    protected void btnSupUpdate_Click(object sender, EventArgs e)
    {
        btnSupNew.Enabled = false;
        btnSupDelete.Enabled = false;
        btnSupEdit.Enabled = false;
        btnSubmit.Enabled = false;
        btnSupUpdate.Enabled = true;


        String SelectValue = DdlSupplierName.SelectedValue;
        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        String sql = "UPDATE SupplierMaster SET  SupName = '" + txtSupName.Text + "' ,SupAddrStreet = '" + txtSupStreet.Text + "' , SupAddrCity ='" + txtSupCity.Text + "', SupState = '" + txtSupState.Text + "' ,SupCountry = '" + txtSupCountry.Text + "', SupZipCode = '" + txtSupZipCode.Text + "', SupPhone= '" + txtSupPhone.Text + "',SupFax=  '" + txtSupFax.Text + "',SupEmailId= '" + txtSupEMail.Text + "', SupLeadTime='" + txtSupLeadTime.Text + "' ,SupStatus='" + RDbtnListSupp.SelectedValue + "'  WHERE [SupID]=" + SelectValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.ExecuteNonQuery();
        con.Close();

        txtSupName.Text = "";
        txtSupStreet.Text = "";
        txtSupCity.Text = "";
        txtSupState.Text = "";
        txtSupCountry.Text = "";
        txtSupZipCode.Text = "";
        txtSupPhone.Text = "";
        txtSupFax.Text = "";
        txtSupEMail.Text = "";
        txtSupLeadTime.Text = "";
       // txtSupStatus.Text = "";
        RDbtnListSupp.SelectedIndex = 0;

    }

    
    
}
